﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Easyui扩展库 Datagrid - 落阳 整理</title>
    <link href="http://www.easyui-extlib.com/Content/Site.css" rel="stylesheet" />
    <script src="http://www.easyui-extlib.com/Scripts/jquery/jquery-1.11.0.min.js"></script>
    <script src="http://www.easyui-extlib.com/Scripts/jquery-extensions/jquery.jdirk.js"></script>
    <link href="http://www.easyui-extlib.com/Scripts/jquery-easyui/jquery-easyui-1.4.3/themes/default/easyui.css" rel="stylesheet" />
    <link href="http://www.easyui-extlib.com/Scripts/jquery-easyui/jquery-easyui-1.4.3/themes/icon.css" rel="stylesheet" />
    <script src="http://www.easyui-extlib.com/Scripts/jquery-easyui/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
    <script src="http://www.easyui-extlib.com/Scripts/jquery-easyui/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js"></script>

    <link href="http://www.easyui-extlib.com//Scripts/jquery-easyui-extensions/validatebox/jeasyui.extensions.validatebox.css" rel="stylesheet" />
    <script src="http://www.easyui-extlib.com//Scripts/jquery-easyui-extensions/validatebox/jeasyui.extensions.validatebox.js"></script>
    <script src="http://www.easyui-extlib.com//Scripts/jquery-easyui-extensions/datagrid/jeasyui.extensions.datagrid.editors.js"></script>
    <script src="http://www.easyui-extlib.com//Scripts/jquery-easyui-extensions/datagrid/jeasyui.extensions.datagrid.edit.autoCount.js"></script>
    <script>
    $(function () {

        var dg = $("#dg1"),
            defaultRow = { ID: "", Code: "", Name: "", StandardModel: "", ApplyCount: "", Remark: "", Stocks: "" },
            insertPosition = "bottom";

        var virData = {
            rows: [
                { ID: "1", Code: "001", Name: "A", StandardModel: "M1", ApplyCount: 12, PriceUnit: 100, TotalCost: 1200, Remark: "", Stocks: 3 },
                { ID: "2", Code: "002", Name: "B", StandardModel: "M2", ApplyCount: 13, PriceUnit: 10, TotalCost: 130, Remark: "", Stocks: 13 },
                { ID: "3", Code: "003", Name: "C", StandardModel: "M3", ApplyCount: 43, PriceUnit: 32, TotalCost: 1376, Remark: "", Stocks: 23 },
                { ID: "4", Code: "003", Name: "D", StandardModel: "M4", ApplyCount: 51, PriceUnit: 19, TotalCost: 969, Remark: "", Stocks: 41 }
            ],
            footer: [
                { Code: "合计", ApplyCount: 119, TotalCost: 3675, Stocks: 80 }
            ]
        };

        var dgInit = function () {

            var getColumns = function () {
                var result = [];

                var normal = [
                    {
                        field: 'Code', title: '物资编码', width: 180,
                        editor: {
                            type: "validatebox",
                            options: {
                                required: true
                            }
                        }
                    },
                    {
                        field: 'Name', title: '名称', width: 180,
                        editor: {
                            type: "validatebox",
                            options: {
                                required: false,
                                readonly: false
                            }
                        }
                    },
                    {
                        field: 'StandardModel', title: '规格型号(只读)', width: 100,
                        editor: {
                            type: "validatebox",
                            options: {
                                required: false,
                                readonly: true
                            }
                        }
                    },
                    {
                        field: 'ApplyCount', title: '申请数量', width: 100, calcable: true,
                        editor: {
                            type: "numberbox",
                            options: {
                                required: true
                            }
                        }
                    },
                    {
                        field: 'Remark', title: '备注', width: 100,
                        editor: {
                            type: "text"
                        }
                    },
                    {
                        field: 'Stocks', title: '库存数量', width: 100, calcable: true,
                        editor: {
                            type: "numberbox",
                            options: {
                                readonly: false
                            }
                        }
                    }
                ];
                result.push(normal);

                return result;
            };
            var options = {
                idField: "ID",
                rownumbers: true,
                fitColumns: true,
                fit: true,
                border: false,
                singleSelect: true,
                columns: getColumns(),
                showFooter: true,
                data: virData,
                onDblClickRow: function (index, row) {
                    dg.datagrid("beginEdit", index);
                }
            };

            dg.datagrid(options);

        };

        var getInsertRowIndex = function () {
            return insertPosition == "top" ? 0 : dg.datagrid("getRows").length;
        }

        var buttonBindEvent = function () {

            $("#btnInsert").click(function () {
                var targetIndex = getInsertRowIndex(), targetRow = $.extend({}, defaultRow, { ID: $.util.guid() });
                dg.datagrid("insertRow", { index: targetIndex, row: targetRow });
                dg.datagrid("beginEdit", targetIndex);
            });

            $("#btnSave").click(function () {
                var rows = dg.datagrid("getRows"), len = rows.length;
                for (var i = 0; i < len; i++) {
                    if (dg.datagrid("isEditing", i)) {
                        dg.datagrid("endEdit", i);
                    }
                }
            });
        };


        dgInit(); buttonBindEvent();


    });
    </script>
</head>
<body>
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'north',border:false" style="height:40px;overflow:hidden;">
            <div class="datagrid-toolbar" style="height:100%;padding:7px;">
                <a id="btnInsert" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">新增一行</a>
                <a id="btnSave" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true">保存数据行(不交互后台)</a>
            </div>
        </div>
        <div data-options="region:'center',border:false">
            <div class="easyui-layout" data-options="fit:true">
                <div data-options="region:'north',border:false" style="height:100px;overflow:hidden;">
                    <div style="font-size:22px;font-weight:bolder;width:100%;text-align:center;">
                        本demo演示本页数据行自动统计功能
                        <br />
                        本demo已开启“申请数量、库存数量”这2个列的自动统计功能
                        <br />
                        已开启双击数据行触发编辑效果的功能
                    </div>
                </div>
                <div data-options="region:'center',border:false" style="border-top-width:1px;">
                    <table id="dg1"></table>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
